<?php
include_once 'MY/extra_sql.php';
function GetID($prefix) {
	//第一步:初始化种子
	//microtime(); 是个数组
	$seedstr =split(" ",microtime(),5);
	$seed =$seedstr[0]*10000;
	//第二步:使用种子初始化随机数发生器
	srand($seed);
	//第三步:生成指定范围内的随机数
	$random =rand(1000,10000);

	$filename = date("Ymd", time()).$random.$prefix;

	return $filename;
}
class TeacherController extends Zend_Controller_Action{
	function init(){        
		if($_SESSION['user']==""){
			echo "<script>window.location='../user/login';</script>"; exit;
		}
		$this->registry = Zend_Registry::getInstance();
		$this->view = $this->registry['view'];
		$this->view->baseUrl = $this->_request->getBaseUrl();
		$this->view->template = "teacher";
		$this->view->conf = new Zend_Config(require './application/configs/config.php');
		$group_id = $_SESSION['user']['group_id'];
		$group = new Group();
		$group = $group->getgroupByid($group_id);
		$groups = explode(',',$group[0]['models']);
		if(!in_array('teacher',$groups)){
			echo "<script>alert('No Permission To Enter!');history.go('-1');</script>";exit();
		}
	}

	public function indexAction(){
		$teacher = new Teacher();
		$teachers = $teacher->getAllTeachers('','','');
		$this->view->total = count($teachers);
		$this->view->top = $teacher->getTopTeachers();
		echo $this->view->render('teacher/index.html');
	}

	//添加教师方法
	public function addAction(){
		$filter=new Zend_Filter_StripTags();
		$teacher = new Teacher();
		$operation = new Operation();
		$department = new Department();
		$this->view->departments = $department->getAlldepartments('','','');
		if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
			$name=$filter->filter(($this->_request->getPost('name')));
			$sex=$filter->filter(($this->_request->getPost('sex')));
			$birthday=$filter->filter(($this->_request->getPost('birthday')));
			$email=$filter->filter(($this->_request->getPost('email')));
			$telephone=$filter->filter(($this->_request->getPost('telephone')));
			$location=$filter->filter(($this->_request->getPost('location')));
			$company=$filter->filter(($this->_request->getPost('company')));
			$summary=$this->_request->getPost('summary');
			$category=$filter->filter(($this->_request->getPost('category')));
			$department_id=$filter->filter(($this->_request->getPost('department_id')));
			$department_name=$filter->filter(($this->_request->getPost('department')));
	        $user_id=$_SESSION['user']['user_id'];
			$birthday = strtotime($birthday);
			$path= "public/files/teacher/".date("Ym",time())."/";//上传路径
			if(!file_exists($path)){
				mkdir("$path", 0700);
			}
			$path= "public/files/teacher/".date("Ym",time())."/".date("d",time())."/";//上传路径
			if(!file_exists($path)){
				mkdir("$path", 0700);
			}
			$file1=GetID( ".".strtolower(substr($_FILES['photo']['name'], (strrpos($_FILES['photo']['name'], '.') + 1))));
			$path = $path.$file1;
			move_uploaded_file($_FILES["photo"]["tmp_name"],$path);
			
		//add teacher department
		    if($department_name!=''){
			    $dat = array(
	                    'name'=>$department_name
	                );
	            $department_id = $department->insert($dat);	
		    }
            $data = array(
				'name'=>$name,
				'sex'=>$sex,
				'birthday'=>$birthday,
				'email'=>$email,
				'telephone'=>$telephone,
				'location'=>$location,
				'company'=>$company,
				'summary'=>$summary,
				'category'=>$category,
				'photo'=>$path,
				'department_id'=>$department_id,
			    'user_id'=>$user_id
			);
	
			$teacher_id = $teacher->insert($data);
			if($teacher_id){
				$time = time();
				$data = array(
					'op_name'=>'add',
					'model_name'=>'teacher',
					'user_id'=>$_SESSION['user']['user_id'],
					'user_name'=>$_SESSION['user']['user_name'],
					'name'=>'增加教师',
					'create_time'=>$time
				);
				$query = $operation->insert($data);
				echo "<script>window.location='detail?teacher_id=".$teacher_id."'</script>";
			}else{
				
			}
		}
		echo $this->view->render('teacher/add.html');
	}

	//修改教师方法
	public function editAction(){
	    $filter=new Zend_Filter_StripTags();
	    $teacher = new Teacher();
	    $department = new Department();
	    $operation = new Operation();
	    $teacher_id=$filter->filter(($this->_request->getParam('teacher_id')));

		if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
			$name=$filter->filter(($this->_request->getPost('name')));
			$sex=$filter->filter(($this->_request->getPost('sex')));
			$birthday=$filter->filter(($this->_request->getPost('birthday')));
			$email=$filter->filter(($this->_request->getPost('email')));
			$telephone=$filter->filter(($this->_request->getPost('telephone')));
			$location=$filter->filter(($this->_request->getPost('location')));
			$company=$filter->filter(($this->_request->getPost('company')));
			$summary=$this->_request->getPost('summary');
			$category=$filter->filter(($this->_request->getPost('category')));
			$department_id=$filter->filter(($this->_request->getPost('department_id')));
			$current_photo=$filter->filter(($this->_request->getPost('current_photo')));
            $photo_del=$filter->filter(($this->_request->getPost('photo_del')));
			$birthday = strtotime($birthday);
			if($_FILES['photo']['name']!=""){
				$path= "public/files/teacher/".date("Ym",time())."/";//上传路径
				if(!file_exists($path))
				{
					mkdir("$path", 0700);
				}//END IF
				$path= "public/files/teacher/".date("Ym",time())."/".date("d",time())."/";//上传路径
				if(!file_exists($path))
				{
					mkdir("$path", 0700);
				}

				$file1=GetID( ".".strtolower(substr($_FILES['photo']['name'], (strrpos($_FILES['photo']['name'], '.') + 1))));
				$path = $path.$file1;
					move_uploaded_file($_FILES["photo"]["tmp_name"],$path);
			} else {
			        if($photo_del=="1"){
						$path = "";
					}else{
						$path = $current_photo;
					}
			}
			$data = array(
				'name'=>$name,
				'sex'=>$sex,
				'birthday'=>$birthday,
				'email'=>$email,
				'telephone'=>$telephone,
				'location'=>$location,
				'company'=>$company,
				'summary'=>$summary,
				'category'=>$category,
				'photo'=>$path,
				'department_id'=>$department_id
			);

			$teacher->updateteacherByteacher_id($data, $teacher_id);
			$time = time();
			$data = array(
				'op_name'=>'edit',
				'model_name'=>'teacher',
				'user_id'=>$_SESSION['user']['user_id'],
				'user_name'=>$_SESSION['user']['user_name'],
				'name'=>'修改教师',
				'create_time'=>$time
			);
			$query = $operation->insert($data);
			echo "<script>window.location='detail?teacher_id=".$teacher_id."'</script>";
		}else{
			$this->view->teacher = $teacher->getteacherByteacher_id($teacher_id);
			$this->view->departments = $department->getAlldepartments('','','');
        	$id = $this->view->teacher[0]['department_id'];
        	if($id!='0'){
	            $department = $department->getdeparmentByid($id);
	            $this->view->department_name = $department[0]['name'];
	        }else{
	            $this->view->department_name="";
	        }
	    }
		echo $this->view->render('teacher/edit.html');
	}


	//教师列表页
	public function listAction(){
		$filter=new Zend_Filter_StripTags();
		$teacher = new Teacher();
		$operation = new Operation();
		$db = new mysql(constant('dbhost'),constant('dbuser'),constant('dbpass'),constant('dbname'),"UTF8");
		$page=$filter->filter(($this->_request->getParam('page')));
		$search_name=$filter->filter(($this->_request->getParam('search_name')));
		if($page==""){
			$page="1";
		}
		$count = $db->select_teachers('','',$search_name);
		//总条数
		$total = count($count);
		//总页数
		$perpage = "10";
		$pages = ceil($total/$perpage);
		$start = ($page-1)*$perpage;
		$end = $perpage;
		$this->view->teachers = $db->select_teachers($start,$end,$search_name);
		$this->view->page = $page;
		$this->view->pages = $pages;
		$this->view->total = $total;
		$time = time();
		$data = array(
			'op_name'=>'list',
			'model_name'=>'teacher',
			'user_id'=>$_SESSION['user']['user_id'],
			'user_name'=>$_SESSION['user']['user_name'],
			'name'=>'取得所有教师',
			'create_time'=>$time
		);
		$query = $operation->insert($data);
		echo $this->view->render('teacher/list.html');
	}

	//教师详情页
	public function detailAction(){

		$filter=new Zend_Filter_StripTags();
		$teacher = new Teacher();
		$class = new Classes();
		$project = new Project();
		$department = new Department();
		$operation = new Operation();
		$teachertag = new TeacherTag();
		$tag = new Tag();
		$db = new mysql(constant('dbhost'),constant('dbuser'),constant('dbpass'),constant('dbname'),"UTF8");
		$page=$filter->filter(($this->_request->getParam('page')));
		$teacher_id=$filter->filter(($this->_request->getParam('teacher_id')));


		$this->view->teacher = $teacher->getteacherByteacher_id($teacher_id);
		$this->view->projects = $project->getprojectByteacher_id($teacher_id);
		$tag_current = $teachertag->getteachertagByteacher_id($teacher_id);
		$arr = array('0'=>'0');
		for($i=0;$i<count($tag_current);$i++){
			$arr[].=$tag_current[$i]['tag_id'];
		}
                   
		$this->view->tag_current = $tag->getAllTagInArray($arr);
		//comment的页数
		if($page==""){
			$page="1";
		}
		//查询所有的comment和发表comment的用户名
		$count = $db->select_comment('','',$teacher_id,'6');

		//总条数
		$total = count($count);

		//总页数
		$perpage = "2";
		$pages = ceil($total/$perpage);
		$start = ($page-1)*$perpage;
		$end = $perpage;
		$this->view->page = $page;
		$this->view->pages = $pages;
		$this->view->total = $total;

		$this->view->comments = $db->select_comment($start,$end,$teacher_id,'6');
		//查询department
		$id = $this->view->teacher[0]['department_id'];
		if($id!='0'){
			$department = $department->getdeparmentByid($id);
			$this->view->department_name = $department[0]['name'];
		}else{
			$this->view->department_name="";
		}
		$time = time();
		$data = array(
			'op_name'=>'detail',
			'model_name'=>'teacher',
			'user_id'=>$_SESSION['user']['user_id'],
			'user_name'=>$_SESSION['user']['user_name'],
			'name'=>'查看教师详情',
			'create_time'=>$time
		);
		$query = $operation->insert($data);
		echo $this->view->render('teacher/detail.html');
	}

	//删除教师
	public function deleteAction(){
		$filter=new Zend_Filter_StripTags();
		$teacher = new Teacher();
		$operation = new Operation();
		$teacher_id = $filter->filter(($this->_request->getParam('teacher_id')));
		$query = $teacher->delteacherByteacher_id($teacher_id);
		if($query){
			$time = time();
			$data = array(
				'op_name'=>'delete',
				'model_name'=>'teacher',
				'user_id'=>$_SESSION['user']['user_id'],
				'user_name'=>$_SESSION['user']['user_name'],
				'name'=>'删除教师',
				'create_time'=>$time
			);
			$query = $operation->insert($data);
			echo "<script>alert('Success');window.location='list'</script>";
		}else{
			echo "<script>alert('Failure')</script>";
		}
	}
	
	public function hoverAction(){
		$filter=new Zend_Filter_StripTags();
		$teacher = new Teacher();
		$search_name=$filter->filter(($this->_request->getParam('search_name')));
	    $this->view->teachers = $teacher->getAllTeachers('','',$search_name);
		echo $this->view->render('teacher/hover.html');
	}
	   
	//删除标签
	public function tagdelAction(){
		$filter=new Zend_Filter_StripTags();
		$teachertag = new TeacherTag();
		$teacher_id=$filter->filter(($this->_request->getParam('teacher_id')));
		$tag_id=$filter->filter(($this->_request->getParam('tag_id')));
		$query = $teachertag->delteachertagByteacherid($teacher_id, $tag_id);
		if($query){
			echo "<script>window.location='tag?teacher_id=".$teacher_id."'</script>";
		}
	}

	public function tagAction(){
		$filter=new Zend_Filter_StripTags();
		$teachertag = new TeacherTag();
		$teacher = new Teacher();
		$tag = new Tag();
		$teacher_id=$filter->filter(($this->_request->getParam('teacher_id')));
		if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
			$teacher_id=$filter->filter(($this->_request->getPost('teacher_id')));
			$tag_id=$_POST['tag_id'];
		
			$count = count($tag_id);
			  if($count!=0){
					for($i=1;$i<=$count;$i++){
						$data=array(
						  'teacher_id'=>$teacher_id,
						  'tag_id'=>$tag_id[$i-1]
						);
						$teachertag->insert($data);
						unset($data);
					}
				   echo "<script>window.location='detail?teacher_id=".$teacher_id."'</script>";
			   }
		}else{
			$this->view->teacher = $teacher->getteacherByteacher_id($teacher_id);
			$tag_current = $teachertag->getteachertagByteacher_id($teacher_id);
			$category=$filter->filter(($this->_request->getParam('category')));
			$search=$filter->filter(($this->_request->getParam('search')));
			$arr = array('0'=>'0');
			for($i=0;$i<count($tag_current);$i++){
				$arr[].=$tag_current[$i]['tag_id'];
			}
			$this->view->tag_current = $tag->getAllTagInArray($arr);
			if(!empty($category)){
				$this->view->tags = $tag->getAllTags('','','','');
			}else if(!empty($search)){
				$this->view->tags = $tag->getAllTags('','', $search,'');
		
			}else{
				$teacher = $this->view->teacher;
				$keys = $teacher[0]['key_word'];
		
				$this->view->tags = $tag->getAllTags('','','', $keys);
			}
		
		}
		echo $this->view->render('teacher/tag.html');
	}
	   
}
?>
